package interface_graphique;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

public class FenetrePrincipale extends JFrame {
	public JTextField e_type;
	public JTextField e_name;
	public JTextField e_size;
	public JTextField e_quantity;
	public JTextField e_dateD;
	public JTextField e_dateM;
	public JTextField e_dateY;
	public JRadioButton perrisable;
	public JButton BouttonOk;
	public int dateOk = 0;
	public Frigo f;
//	public Canvas canvas;

	public FenetrePrincipale() {
		super("I-Fridge");

	    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setContentPane(new JLabel(new ImageIcon("backF.png")));
	    this.setLayout(new BorderLayout());
	    f = new Frigo();
	    Canvas canvas = new Canvas(f);
	    canvas.setOpaque(false);
	    this.getContentPane().add( canvas, BorderLayout.CENTER);
				 
		JPanel Contecrire= new JPanel();
		Contecrire.setLayout(new BoxLayout(Contecrire, BoxLayout.X_AXIS));
		Contecrire.add(Box.createRigidArea(new Dimension(122, 0)));
		
				
		JPanel ecrire = new JPanel();
		ecrire.setLayout(new BoxLayout(ecrire, BoxLayout.Y_AXIS));
		

		e_name = new JTextField("",1);
		e_name.setBorder(null);
		e_name.setBackground(Color.ORANGE);
		e_type = new JTextField("",1); 
		e_type.setBorder(null);
		e_type.setBackground(Color.green);
		e_size = new JTextField("",1);
		e_size.setBorder(null);
		e_size.setBackground(Color.magenta);
		e_quantity= new JTextField("",1);
		e_quantity.setBorder(null);
		e_quantity.setBackground(Color.ORANGE);
		ImageIcon img = new ImageIcon("boutton.png");
	 
		BouttonOk = new JButton(img);
		BouttonOk.setBorderPainted(false); 
		perrisable = new JRadioButton("Perissable");
		  	 
		e_dateD= new JTextField("",3);
		e_dateM= new JTextField("",3);
		e_dateY= new JTextField("",3);
		e_dateD.setBorder(null);
		e_dateD.setBackground(Color.BLUE);
		 
		e_dateM.setBorder(null);
		e_dateM.setBackground(Color.white);
		e_dateY.setBorder(null);
		e_dateY.setBackground(Color.RED);
		
		
		JPanel paneDate = new JPanel();
		 
		 paneDate = new JPanel();
		 paneDate.add(e_dateD);
		 paneDate.add(e_dateM);
		 paneDate.add(e_dateY);
		 paneDate.setOpaque(false);
		 paneDate.setBorder(null);

		 ecrire.add(Box.createRigidArea(new Dimension(170, 147)));
	 
		ecrire.add(e_name);
		ecrire.add(Box.createRigidArea(new Dimension(0, 17)));
		ecrire.add(e_type);
		ecrire.add(Box.createRigidArea(new Dimension(0, 12)));
		ecrire.add(e_size);
		ecrire.add(Box.createRigidArea(new Dimension(0, 11)));
		ecrire.add(e_quantity);
		ecrire.add(Box.createRigidArea(new Dimension(0, 11)));
		
		
		ecrire.add(paneDate);
		ecrire.add(perrisable);
		 
		ecrire.setOpaque(false);
		 
		
		this.repaint();
		this.revalidate();
		
		Contecrire.setOpaque(false);
		 
		Contecrire.add(ecrire);
		
		JPanel ConteAjout= new JPanel();
		ConteAjout.setLayout(new BoxLayout(ConteAjout, BoxLayout.Y_AXIS));
		ConteAjout.setOpaque(false);
		ConteAjout.add(Contecrire);
		ConteAjout.add(Box.createRigidArea(new Dimension(0, 10)));
		
		ConteAjout.add(BouttonOk);
		
		BouttonOk.setOpaque(false);
		ConteAjout.add(Box.createRigidArea(new Dimension(0, 550)));

		this.getContentPane().add(ConteAjout, BorderLayout.WEST);

		  
		perrisable.addActionListener(radioListener);
		BouttonOk.addActionListener(Ok);
		canvas.repaint();
		this.setSize(1000, 750);
		this.setResizable(false);
		this.setVisible(true);
		  
	  }
	
	private  ActionListener radioListener = new ActionListener(){
		public void actionPerformed(ActionEvent e) {
			if(perrisable.isSelected()){
			 
				dateOk = 1;
			}
			else{
				 
				dateOk=0;
				}
			
		 
	}
		
			 			 
	};
	
	private  ActionListener Ok= new ActionListener(){
		public void actionPerformed(ActionEvent e) {
		
				if(dateOk==0){
				int size;
				int nb;
				Date dateActuelle = new Date();
				String S = new String();
				E_Type type = E_Type.Unreferenced;
				String nom = new String();
				
				nom=e_name.getText();
				size=Integer.parseInt(e_size.getText());
				nb=Integer.parseInt(e_quantity.getText());
				S=e_type.getText();
				
				switch (S) {
				case "Unreferenced":
					type = E_Type.Unreferenced;
					break;
				case "Dairy":
					type = E_Type.Dairy;
					break;
				case "Drink":
					type = E_Type.Drink;
					break;
				case "Meat":
					type = E_Type.Meat;
					break;
				case "Fish":
					type = E_Type.Fish;
					break;
				case "Eggs":
					type = E_Type.Eggs;
					break;
				case "Vegetable":
					type = E_Type.Vegetable;
					break;
				case "Fruit":
					type = E_Type.Fruit;
					break;
				}
				
				
				Produit p= new Produit(type,nom,size,nb,dateActuelle);
				 Action action= new Action();
				 action.AjoutSansDate(p,f.inventaire);
				 e_name.setText("");
				 e_quantity.setText("");
				 e_size.setText("");
				 e_type.setText("");
				 p.toString();
				 
				 
				}
				else{
					
					int size;
					int nb;
					Date dateActuelle = new Date();
					String S = new String();
					E_Type type = E_Type.Unreferenced;
					String nom = new String();
					
					nom=e_name.getText();
					size=Integer.parseInt(e_size.getText());
					nb=Integer.parseInt(e_quantity.getText());
					S=e_type.getText();
					Date date = new Date();
					date.setYear(Integer.parseInt(e_dateY.getText())); 
					date.setMonth(Integer.parseInt(e_dateM.getText())); 
					date.setDate(Integer.parseInt(e_dateD.getText())); 
					switch (S) {
					case "Unreferenced":
						type = E_Type.Unreferenced;
						break;
					case "Dairy":
						type = E_Type.Dairy;
						break;
					case "Drink":
						type = E_Type.Drink;
						break;
					case "Meat":
						type = E_Type.Meat;
						break;
					case "Fish":
						type = E_Type.Fish;
						break;
					case "Eggs":
						type = E_Type.Eggs;
						break;
					case "Vegetable":
						type = E_Type.Vegetable;
						break;
					case "Fruit":
						type = E_Type.Fruit;
						break;
					}
					
					
					P_Avec_Date p= new P_Avec_Date(type,nom,size,nb,date,dateActuelle);
					 Action action= new Action();
					 action.AjoutSansDate(p,f.inventaire);
					 e_name.setText("");
					 e_quantity.setText("");
					 e_size.setText("");
					 e_type.setText("");
					 e_dateD.setText("");
					 e_dateM.setText("");
					 e_dateY.setText("");
					 p.toString();
					
					
				}
			} 			 			 
	};
	
	public static void main(String[] args) {
		FenetrePrincipale frame = new FenetrePrincipale();

	}	    
}

